package com.qq.aidoctor;

import com.alibaba.dashscope.assistants.Assistant;
import com.qq.aidoctor.assistant.QWenAssistant;
import com.qq.aidoctor.assistant.SeparateChatAssistant;
import com.qq.aidoctor.bean.Appointment;
import com.qq.aidoctor.service.AppointmentService;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.spring.AiService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;

@SpringBootTest
class AiDoctorApplicationTests {

    @Resource
    private OpenAiChatModel model;

    @Resource
    private QwenChatModel qwenModel;

    @Resource
    private QWenAssistant qwAssistant;

    @Test
    void contextLoads() {
        OpenAiChatModel model = OpenAiChatModel.builder()
                .baseUrl("http://langchain4j.dev/demo/openai/v1")
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();
        String answer = model.chat("Say 'Hello World'");
        System.out.println(answer); // Hello World
    }

    @Test
    void test() {
        String answer = model.chat("你好！");
        System.out.println(answer);
    }

    @Test
    void aliTest() {
        String answer = qwenModel.chat("你好！你是什么模型？");
        System.out.println(answer);

    }

    @Test
    void aliWXTest() {
        WanxImageModel wanxImageModel = WanxImageModel.builder()
                .modelName("wanx2.1-t2i-plus")
                .apiKey("sk-a527304190714ffa861c517832c7b28b")
                .build();
        Response<Image> response = wanxImageModel.generate("图片风格为“肖像摄影”。请在镜子前画一张非常普通的iPhone自拍照。主角是一位穿着透明薄膜的性感美女，身高175的模特身材，长相清纯，长得像刘亦菲。美女位于图片中心，坐在地上，双腿呈M形。一条腿在身体右侧，上面覆盖着大量的沐浴露，脸上不小心沾上了沐浴露，湿漉漉的头发痕迹，赤脚放在房间的地板上，用后置摄像头在房间的地镜前自拍。照片打开时带有闪光灯，由于快门速度不足而略微模糊，构图混乱。整张照片呈现出平庸的日常感，比例为“4:3”。");
        System.out.println(response.content().url());
    }

    @Test
    void AiServiceTest() {
        String answer = qwAssistant.chat("你好！");
        System.out.println(answer);
    }

    @Test
    void SimpleMemoryTest() {
        //第一轮对话
        UserMessage userMessage1 = UserMessage.userMessage("我是环环");
        ChatResponse chatResponse1 = qwenModel.chat(userMessage1);
        AiMessage aiMessage1 = chatResponse1.aiMessage();
//输出大语言模型的回复
        System.out.println(aiMessage1.text());
//第二轮对话
        UserMessage userMessage2 = UserMessage.userMessage("你知道我是谁吗");
        ChatResponse chatResponse2 = qwenModel.chat(Arrays.asList(userMessage1,
                aiMessage1, userMessage2));
        AiMessage aiMessage2 = chatResponse2.aiMessage();
//输出大语言模型的回复
        System.out.println(aiMessage2.text());

    }

    @Test
    void AIServiceMemoryTest() {
        //创建chatMemory
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        //创建AIService
        QWenAssistant assistant = AiServices
                .builder(QWenAssistant.class)
                .chatModel(qwenModel)
                .chatMemory(chatMemory)
                .build();
        //调用service的接口
        String answer1 = assistant.chat("我是环环");
        System.out.println(answer1);
        String answer2 = assistant.chat("我是谁");
        System.out.println(answer2);

    }

    @Test
    void DIAIServiceMemoryTest() {
        String answer1 = qwAssistant.chat("我是环环");
        System.out.println(answer1);
        String answer2 = qwAssistant.chat("我是谁");
        System.out.println(answer2);

    }


    @Autowired
    private SeparateChatAssistant separateChatAssistant;
    @Test
    public void testChatMemory5() {
        String answer1 = separateChatAssistant.chat(1,"我是环环");
        System.out.println(answer1);
        String answer2 = separateChatAssistant.chat(1,"我是谁");
        System.out.println(answer2);
        String answer3 = separateChatAssistant.chat(2,"我是谁");
        System.out.println(answer3);


    }


    @Autowired
    private AppointmentService appointmentService;
    @Test
    void testGetOne() {
        Appointment appointment = new Appointment();
        appointment.setUsername("张三");
        appointment.setIdCard("123456789012345678");
        appointment.setDepartment("内科");
        appointment.setDate("2025-04-14");
        appointment.setTime("上午");
        Appointment appointmentDB = appointmentService.getOne(appointment);
        System.out.println(appointmentDB);
    }

    @Test
    void testSave() {
        Appointment appointment = new Appointment();
        appointment.setUsername("张三");
        appointment.setIdCard("123456789012345678");
        appointment.setDepartment("内科");
        appointment.setDate("2025-04-14");
        appointment.setTime("上午");
        appointment.setDoctorName("张医生");
        appointmentService.save(appointment);
    }
    @Test
    void testRemoveById() {
        appointmentService.removeById(1L);
    }



}
